//
//  OwnweController.m
//  PropertyMananger
//
//  Created by bh on 17/1/11.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "OwnweController.h"
#import "MCSearchBar.h"
#import "OwnerInfoCell.h"
#import "OwnerModel.h"
#import "OnweDetailController.h"

@interface OwnweController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>


//业主信息数据源
@property (nonatomic,strong) NSMutableArray * ownerData;
@end
static NSString * headerID = @"headerID";
@implementation OwnweController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"业主信息查询"];
    self.view.backgroundColor = [UIColor whiteColor];
    self.nextPage = 1;
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 70, BHscreenW, BHscreenH - 64 - 70) isGroup:NO];
    
     [self setupSearchBar];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.userInteractionEnabled = YES;
    self.view.userInteractionEnabled = YES;
    //数据请求zhu
    [self postOwnerInfoData];
    
    [self createRefresh];
   
    
}
#pragma mark - 创建刷新UI
- (void)createRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.nextPage = 1;
        
        self.isDown = YES;
        //获取数据
        [self postOwnerInfoData];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.nextPage += 1;
        
        self.isDown = NO;
        //获取数据
        [self postOwnerInfoData];
        
    //    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }];
}
#pragma mark - 搜索框
- (void)setupSearchBar
{
    self.searchBar = [[MCSearchBar alloc] initWithFrame:CGRectMake(15, 15, BHscreenW - 30, 35) placeholder:@"请输入业主姓名"];
    self.searchBar.delegate = self;
    self.searchBar.barTintColor = [UIColor whiteColor];
    [self.view addSubview:self.searchBar];
    [self.searchBar setBackgroundColor:[UIColor whiteColor]];
    self.searchBar.layer.cornerRadius = 5.0f;
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.borderWidth = 0.5;
    self.searchBar.layer.borderColor = BHColor(102, 102, 102).CGColor;
    self.searchBar.backgroundColor = [UIColor whiteColor];
    self.searchBar.searchTextField.backgroundColor = [UIColor whiteColor];
    self.searchText = @"";
    
    self.searchBar.searchBarStyle = UISearchBarStyleDefault;
    
  //  _arr = [[NSMutableArray alloc] initWithObjects:@"1",@"12",@"1233",@"21",@"31",@"41",@"55",@"67",@"90",@"1211111",@"900000",@"10", nil];
    self.resultArr = [[NSMutableArray alloc]init];
    self.ownerData = [[NSMutableArray alloc]init];
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isSearch) {
        return self.resultArr.count ;
    }else{
        return self.ownerData.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    OwnerInfoCell * cell = [OwnerInfoCell cellWithTableView:tableView];
    
    if (self.isSearch) {
        OwnerModel * model = self.ownerData[indexPath.row];
        cell.nameLabel.text = model.OwnerName;
        NSArray * array = model.roomInfoArray;
        NSMutableString * mutlStr = [[NSMutableString alloc]init];
        
        for (OwnerRoomInfoData * model1 in array) {
            
            
            //     DLog(@" === %@",model1.LongRoomName);
            [mutlStr appendFormat:@"  %@",model1.LongRoomName];
            
        }
        cell.subtitle.text = mutlStr;
        
    }else{
        OwnerModel * model = self.ownerData[indexPath.row];
        cell.nameLabel.text = model.OwnerName;
        NSArray * array = model.roomInfoArray;
        NSMutableString * mutlStr = [[NSMutableString alloc]init];
        
        for (OwnerRoomInfoData * model1 in array) {
            
         
       //     DLog(@" === %@",model1.LongRoomName);
            [mutlStr appendFormat:@"  %@",model1.LongRoomName];
           
        }
    cell.subtitle.text = mutlStr;
        
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"点击了=== ");
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OnweDetailController * ownerDetailVC = [[OnweDetailController alloc]init];
    OwnerModel * model = self.ownerData[indexPath.row];

      ownerDetailVC.model = model;

    [self.navigationController pushViewController:ownerDetailVC animated:YES];
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    
    return  headerView;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 10;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    if (searchText.length == 0) {
        self.searchText = @"";
        self.isSearch = NO;
        [self.tableView reloadData];
        
        [self.resultArr removeAllObjects];
        [self.ownerData removeAllObjects];
        
        [self.tableView.mj_header beginRefreshing];
    }
//    else{
//       
//        [self.tableView.mj_header beginRefreshing];
//    }
    NSLog(@" --- %@",searchText);
    self.searchStr = searchText;
    
    [self.resultArr removeAllObjects];
     [self.ownerData removeAllObjects];
    
    [self.tableView reloadData];

    
    for ( OwnerModel * model in self.ownerData) {
        if ([model.OwnerName rangeOfString:searchText].location != NSNotFound) {
            [self.resultArr addObject:model.OwnerName];
        }
    }
    if (self.resultArr.count) {
        self.isSearch = YES;
        [self.tableView reloadData];
    }
    
}


-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    self.isDown = YES;
    
    [self.tableView.mj_header beginRefreshing];
    
    [self postOwnerInfoData];
    
    [self.view endEditing:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    
    self.isDown = YES;
    [self.ownerData removeAllObjects];
    
    [self.searchBar resignFirstResponder];
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    
    self.isDown = YES;
    self.nextPage = 1;
    
    [self postOwnerInfoData];
    self.isSearch = NO;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.tableView endEditing:YES];
    [self.view resignFirstResponder];
    [self.view endEditing:YES];
    self.isSearch = NO;
  //  [self.tableView reloadData];
}
#pragma mark - 获取业主信息数据
- (void)postOwnerInfoData
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    
    NSDictionary * parms = nil;
    if (self.searchStr == nil) {
        
        parms = @{ @"CommunityID":userModel.CommunityID,
                                  @"OwnerName": @"",
                                  @"Page":[NSString stringWithFormat:@"%ld",self.nextPage],
                                  @"json":@"\{\"jsonEntity\":\{},\"action\":\"getownerinfo\",\"version\":\"V1.0\"}"};
    }else {
        
        parms = @{ @"CommunityID":userModel.CommunityID,
                                  @"OwnerName": self.searchStr,
                                  @"Page":[NSString stringWithFormat:@"%ld",self.nextPage],
                                  @"json":@"\{\"jsonEntity\":\{},\"action\":\"getownerinfo\",\"version\":\"V1.0\"}"};

    }
    DLog(@"%@",parms);
    [HomeNetWorkRequest postOwnerQuerySuccess:^(id data) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"业主信息的数据 == %@",array);

        if (self.isDown == YES) {
                
                [self.ownerData removeAllObjects];
                self.isDown = NO;
            }
        
        for (NSDictionary * dict in array) {
            
            OwnerModel * model = [[OwnerModel alloc]init];
            [model setValuesForKeysWithDictionary:dict];
            [self.ownerData addObject:model];
            DLog(@"model.OwnerName == %@",model.OwnerName);
            
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];

    } Fail:^(NSString *errorDes) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUD];
    } param:parms];

}
@end
